package com.hue.gulimail.gulimailproduct.service.impl;

import com.hue.gulimail.gulimailproduct.entity.BrandEntity;
import com.hue.gulimail.gulimailproduct.entity.CategoryEntity;
import com.hue.gulimail.gulimailproduct.service.BrandService;
import com.hue.gulimail.gulimailproduct.service.CategoryService;
import com.hue.gulimail.gulimailproduct.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hue.common.utils.PageUtils;
import com.hue.common.utils.Query;

import com.hue.gulimail.gulimailproduct.dao.CategoryBrandRelationDao;
import com.hue.gulimail.gulimailproduct.entity.CategoryBrandRelationEntity;
import com.hue.gulimail.gulimailproduct.service.CategoryBrandRelationService;


/**
 * @author yfq
 */
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveCategoryBrandRelation(CategoryBrandRelationEntity categoryBrandRelationEntity) {
        //查询brand名和catelog名
        BrandEntity brand = brandService.getById(categoryBrandRelationEntity.getBrandId());
        CategoryEntity catelog = categoryService.getById(categoryBrandRelationEntity.getCatelogId());
        categoryBrandRelationEntity.setBrandName(brand.getName());
        categoryBrandRelationEntity.setCatelogName(catelog.getName());
        save(categoryBrandRelationEntity);
    }

    @Override
    public List<BrandVo> brandList(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> relationWrapper = new QueryWrapper<>();
        relationWrapper.eq("catelog_id", catId);
        List<CategoryBrandRelationEntity> relations = list(relationWrapper);
        List<BrandVo> brandVos =  new ArrayList<>();
        for(CategoryBrandRelationEntity relation:relations){
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(relation.getBrandId());
            brandVo.setBrandName(relation.getBrandName());
            brandVos.add(brandVo);
        }
        return brandVos;
    }

}
